package io.jft.doll.ui.bills.fragment

import android.arch.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.g.base.api.ErrorCode
import com.g.base.extend.observeEx
import com.g.base.extend.resultNotNull
import com.g.base.extend.toast
import com.g.base.room.repository.ListStatus
import com.g.base.room.repository.Status
import com.g.base.ui.BaseFragment
import com.g.base.ui.recyclerView.MultiTypeAdapter
import com.g.base.ui.recyclerView.decoration.LinearItemDecoration
import com.g.base.ui.recyclerView.item.BaseItem
import com.g.base.ui.recyclerView.setupRecyclerView
import io.jft.doll.R
import io.jft.doll.databinding.ViewRecycleBinding
import io.jft.doll.model.viewModel.UserViewModel
import io.jft.doll.room.entity.remote.BillsHistoryREntity
import io.jft.doll.ui.bills.bind.BillsHistoryBind

/**
 * Created by G on 2017/12/4 0004.
 */
class BillsListFragment : BaseFragment<ViewRecycleBinding>() {
    override fun setContentView(): Int = R.layout.view_recycle

    private val userViewModel by lazy { ViewModelProviders.of(this).get(UserViewModel::class.java) }
    private lateinit var adapter: MultiTypeAdapter
    private val data = ArrayList<BillsHistoryREntity>()
    private var status = ListStatus.Content

    @JvmField
    @Autowired
    var type = ""

    override fun onLazyLoadOnce() {
        ARouter.getInstance().inject(this)
        initView()
        obsData()
        userViewModel.operateBills(type, data.size)
        showContentView()
    }

    private fun initView() {
        adapter = setupRecyclerView(contentView.recyclerView)
        contentView.recyclerView.addItemDecoration(LinearItemDecoration(activity!!))

        adapter.setOnLoadingListener {
            if (status != ListStatus.Content) {
                toast("请等待其他 加载操作完成")
                adapter.setLoadingFailed()
            } else {
                userViewModel.operateBills(type, data.size)
                status = ListStatus.LoadMore
            }
        }
    }

    override fun onReload() {
        userViewModel.operateBills(type, 0)
        status = ListStatus.Content
    }

    private fun obsData() {
        userViewModel.obsBillsHistory()
                .resultNotNull()
                .observeEx(this) {
                    when (it.status) {
                        Status.Loading -> {
                            if (status == ListStatus.Content) showLoading()
                        }
                        Status.Content -> {
                            if (it.data!!.size < userViewModel.billsLimit)
                                adapter.setLoadingNoMore()
                            else
                                adapter.setLoadingSucceed()

                            if (status != ListStatus.LoadMore) {
                                data.clear()
                            }

                            data.addAll(it.data!!)
                            applyData()
                            showContentView()
                            status = ListStatus.Content
                        }
                        Status.Error -> {
                            if (status == ListStatus.LoadMore) {
                                when {
                                    it.error?.code == ErrorCode.EMPTY -> {
                                        adapter.setLoadingNoMore()
                                    }
                                    else -> {
                                        adapter.setLoadingFailed()
                                    }
                                }
                            } else {
                                if (it.error?.code == ErrorCode.EMPTY) {
                                    showEmpty(it.error?.message)
                                } else {
                                    showError(it.error?.message)
                                }
                            }
                            status = ListStatus.Content
                        }
                    }
                }
    }

    private fun applyData() {
        adapter.diffItems.addAll(data.map { BillsHistoryBind(it) })

        adapter.calcDiffAndDispatchUpdate(
                { baseItem: BaseItem<*>, i: Int, baseItem1: BaseItem<*>, i1: Int ->
                    (baseItem as BillsHistoryBind).entity.date == (baseItem1 as BillsHistoryBind).entity.date
                },
                { baseItem: BaseItem<*>, i: Int, baseItem1: BaseItem<*>, i1: Int ->
                    (baseItem as BillsHistoryBind).entity.date == (baseItem1 as BillsHistoryBind).entity.date
                })
    }

}